<template>
	<view class="student-course-container">
		<cc-navbar :title="navbarTitle"></cc-navbar>
		<view class="list" v-if="count">
			<block v-for="(row, index) in list" :key="index">
				<view class="item">
					<view class="item-top fcb">
						<view class="u-line-1 item-top-left">
							{{ row.course_name }}
						</view>
						<view v-if="row.statusData">
							<u-text :type="row.statusData.type" :text="row.statusData.label"></u-text>
						</view>
					</view>
					<view class="item-content">
						<view class="tags fc mb10">
							<view class="tag" v-if="row.cla_id">
								<u-tag shape="circle" size="mini" type="primary" :text="row.class_name"></u-tag>
							</view>
							<view class="tag">
								<u-tag plain plainFill shape="circle" size="mini" type="primary"
									:text="row.feeModeLabel"></u-tag>
							</view>
						</view>
						<view class="item-info">
							<view class="font-info">总课时/天:</view>
							<view class="u-line-2">{{ row.total_hour }}</view>
						</view>
						<view class="item-info">
							<view class="font-info">剩余课时/天:</view>
							<view class="u-line-2">{{ row.balance_hour }}</view>
						</view>
					</view>
				</view>
			</block>
		</view>
		<cc-loadmore :loading="loading" :current="list.length" :count="count" />
		<cc-empty v-if="!loading && !count" mode="list" />
	</view>
</template>

<script>
	import {
		getStudentCourse
	} from '@/codecat/api/education/student/course';
	import {
		getFeeMode,
		getDateType,
		getFeeModeUnit,
		getStudentCourseStatus
	} from '@/codecat/config';
	export default {
		// 学员报读课程
		name: 'StudentCourse',
		data() {
			return {
				navbarTitle: '',
				loading: false,
				list: [],
				count: 0,
				query: {
					student_id: '',
					page: 1,
					limit: 10
				}
			};
		},
		onLoad() {
			const options = this.$Route.query;
			if (options.id) {
				this.query.student_id = options.id;
				this.getList(1);
			}
		},
		onReachBottom() {
			if (this.list.length < this.count) {
				this.getList();
			}
		},
		methods: {
			getList(first) {
				const query = this.$lodash.cloneDeep(this.query);
				if (first) {
					query.page = 1;
					this.list = [];
					this.count = 0;
				} else {
					query.page += 1;
				}
				this.loading = true;
				getStudentCourse(query)
					.then((res) => {
						this.loading = false;
						this.query.page = query.page;
						const {
							list,
							count
						} = res.data;
						this.count = count || 0;
						if (Array.isArray(list) && list.length) {
							this.$lodash.forEach(list, (e) => {
								e.feeMode = getFeeMode(e.mode);
								e.dateType = getDateType(e.data_type);
								e.feeModeLabel = e.dateType ? `${e.feeMode}(${e.dateType})` : `${e.feeMode}`;
								e.feeModeUnit = getFeeModeUnit(e.mode, e.data_type);
								e.statusData = getStudentCourseStatus(e.status);

								const {
									mode
								} = e;
								const unit = mode === 'date' ? '(天)' : '(课时)';
								e.balance_hour = `${e.balance_hour}${unit}`;
								e.total_hour = `${e.total_hour}${unit}`;
							});
							this.list = this.list.concat(list);

							if (query.page === 1) {
								this.navbarTitle = `${this.list[0].student_name}-报读课程`;
							}
						}
					})
					.catch((_) => {
						this.loading = false;
					});
			}
		}
	};
</script>

<style lang="scss" scoped>
	@import './index.scss';
</style>